﻿using System;
using System.Threading.Tasks;

namespace RuoVea.ExCache.Cache
{
    /// <summary>
    /// Redis缓存
    /// </summary>
    public class RedisCache : ICache
    {
        /// <summary>
        /// 前缀
        /// </summary>
        private string _prefix = "RuoVea";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="prefix">键值前缀</param>
        /// <param name="redisConnectionString">链接字符串</param>
        public RedisCache(string prefix, string redisConnectionString)
        {
            var csredis = new CSRedis.CSRedisClient(redisConnectionString);
            _prefix = prefix + ":";
            RedisHelper.Initialization(csredis);
        }

        #region Del
        /// <summary>
        /// 用于在 key 存在时删除 key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>long</returns>
        public long Del(params string[] key)
        {
            return RedisHelper.Del(_prefix + key);
        }

        /// <summary>
        /// 用于在 key 存在时删除 key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>long</returns>
        public Task<long> DelAsync(params string[] key)
        {
            return RedisHelper.DelAsync(_prefix + key);
        }

        /// <summary>
        /// 用于在 key 模板存在时删除  删除带该前缀的数据
        /// </summary>
        /// <param name="pattern">pattern为前缀key: pattern* , 含有pattern的key: *pattern*</param>
        /// <returns>bool</returns>
        public async Task<long> DelByProfixAsync(string pattern)
        {
            if (string.IsNullOrEmpty(pattern))
                return default;
            pattern = _prefix + pattern + "*";

            //pattern = Regex.Replace(pattern, @"\{.*\}", "*");
            var keys = (await RedisHelper.KeysAsync(pattern));
            if (keys != null && keys.Length > 0)
            {
                return await RedisHelper.DelAsync(keys);
            }

            return default;
        }
        #endregion

        #region Exists
        /// <summary>
        /// 检查给定 key 是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>bool</returns>
        public bool Exists(string key)
        {
            key = _prefix + key;
            return RedisHelper.Exists(key);
        }

        /// <summary>
        /// 检查给定 key 是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>bool</returns>
        public Task<bool> ExistsAsync(string key)
        {
            key = _prefix + key;
            return RedisHelper.ExistsAsync(key);
        }
        #endregion

        #region Read
        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>string</returns>
        public string Read(string key)
        {
            key = _prefix + key;
            return RedisHelper.Get(key);
        }

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <typeparam name="T">byte[] 或其他类型</typeparam>
        /// <param name="key">键</param>
        /// <returns>T</returns>
        public T Read<T>(string key)
        {
            key = _prefix + key;
            return RedisHelper.Get<T>(key);
        }

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>string</returns>
        public Task<string> ReadAsync(string key)
        {
            key = _prefix + key;
            return RedisHelper.GetAsync(key);
        }

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <typeparam name="T">byte[] 或其他类型</typeparam>
        /// <param name="key">键</param>
        /// <returns>T</returns>
        public Task<T> ReadAsync<T>(string key)
        {
            key = _prefix + key;
            return RedisHelper.GetAsync<T>(key);
        }
        #endregion

        #region Write
        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>bool</returns>
        public bool Write(string key, object value)
        {
            key = _prefix + key;
            return RedisHelper.Set(key, value);
        }

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expire">有效期</param>
        /// <returns>bool</returns>
        public bool Write(string key, object value, TimeSpan expire)
        {
            key = _prefix + key;
            return RedisHelper.Set(key, value, expire);
        }

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>bool</returns>
        public Task<bool> WriteAsync(string key, object value)
        {
            key = _prefix + key;
            return RedisHelper.SetAsync(key, value);
        }

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expire">有效期</param>
        /// <returns>bool</returns>
        public Task<bool> WriteAsync(string key, object value, TimeSpan expire)
        {
            key = _prefix + key;
            return RedisHelper.SetAsync(key, value, expire);
        }
        #endregion

        /// <summary>
        /// 从缓存里取数据，如果不存在则执行查询方法，
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="GetFun">查询方法</param>
        /// <param name="timeSpanMin">有效期 单位分钟/param>
        /// <returns></returns>
        public T Cof_ReadWrite<T>(string key, Func<T> GetFun, TimeSpan timeSpanMin) where T : class
        {
            var obj = Read<T>(key);
            obj = GetFun();
            if (obj == null)
            {
                obj = GetFun();
                Write(key, obj, timeSpanMin);
            }
            return obj as T;
        }
    }
}